package com.google.zxing.client.android.result;

import java.util.ArrayList;
import java.util.Hashtable;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class XMLParser {

	public XMLParser() {
	}

	// List of element in tagName
	private ArrayList<ArrayList<Hashtable<String, String>>> lstAllQuestion 
		= new ArrayList<ArrayList<Hashtable<String,String>>>();
	
	// Key for getting value
	String TAG_QUESTION = "question";
	String TAG_LISTANSWER = "listAnswer";
	String TAG_ANSWER = "answer";
	String TAG_CONTENT = "content";

	public ArrayList<ArrayList<Hashtable<String, String>>> getAllQuestion () {
		return lstAllQuestion;
	}
	
	public void Parse(Document doc) {

		// get element according to tag name in xml file
		NodeList lstQuesNode = doc.getElementsByTagName(TAG_QUESTION);
		Hashtable<String, String> hashAnswer = new Hashtable<String, String>();
		Hashtable<String, String> hashQuestionType = new Hashtable<String, String>();
		ArrayList<Hashtable<String, String>> lstQuesAns = new ArrayList<Hashtable<String,String>>();
		
		//lstQuesNode contain all node <question> in xml file
		for (int i = 0; i < lstQuesNode.getLength(); i++) {
			
			// variable for getting value
			hashAnswer = new Hashtable<String, String>();
			hashQuestionType = new Hashtable<String, String>();
			lstQuesAns = new ArrayList<Hashtable<String,String>>();
			
			// get information in one <question> node
			Node quesNode = lstQuesNode.item(i);
			Element qElement = (Element) quesNode;
			hashQuestionType.put(qElement.getAttribute("level"),
					getValue(TAG_CONTENT, qElement).get(""));

			// lstAnswer = all <listAnswer> node in <question> node
			NodeList lstAnswer = qElement.getElementsByTagName(TAG_LISTANSWER);
			
			// normally, there is one node in lstAnswer
			for (int j = 0; j < lstAnswer.getLength(); j++) {
				Node ansNode = lstAnswer.item(j);
				Element aElement = (Element) ansNode;
				// lstAns: all value of tag <answer> in <listAnwer>
				hashAnswer = getValue(TAG_ANSWER, aElement);
			}
			
			// get information
			lstQuesAns.add(hashQuestionType);
			lstQuesAns.add(hashAnswer);
			lstAllQuestion.add(lstQuesAns);
			
		}
	}

	// variable for getting information in node <listAnswer>
	Hashtable<String, String> lstAnsValue = new Hashtable<String, String>();
	
	private Hashtable<String, String> getValue(String tag, Element element) {
		lstAnsValue = new Hashtable<String, String>();
		NodeList nodes;
		
		// get info in tag <answer>
		for (int i = 0; i < element.getElementsByTagName(tag).getLength(); i++) {
			nodes = element.getElementsByTagName(tag).item(i).getChildNodes();

			// get attribute value in tag <answer>
			NodeList a = element.getElementsByTagName(tag);
			Node aNode = a.item(i);
			Element ele = (Element) aNode;
			Node node = (Node) nodes.item(0);
			
			// put info to hashtable lstAnsValue
			lstAnsValue.put(ele.getAttribute("value"), node.getNodeValue());
		}

		return lstAnsValue;
	}
}
